package com.ngm.tea.tea.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ngm.tea.BaseController;
import com.ngm.tea.tea.entity.Foods;
import com.ngm.tea.tea.entity.Goods;
import com.ngm.tea.tea.entity.Ratings;
import com.ngm.tea.tea.entity.Seller;
import com.ngm.tea.tea.entity.SellerRatings;
import com.ngm.tea.tea.service.FoodsService;
import com.ngm.tea.tea.service.GoodsService;
import com.ngm.tea.tea.service.RatingsService;
import com.ngm.tea.tea.service.SellerRatingsService;
import com.ngm.tea.tea.service.SellerService;

/**
 * 商品大类控制器
 * 
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 11:04:10
 *
 */
@Controller
@RequestMapping(value = "/api")
public class ApiController extends BaseController {

	public static final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");

	@Resource
	private GoodsService thisService;
	@Resource
	private FoodsService foodsService;
	@Resource
	private RatingsService ratingsService;
	@Resource
	private SellerRatingsService sellerRatingsService;
	@Resource
	private SellerService sellerService;

	@ResponseBody
	@RequestMapping("/goods")
	public Map<String, Object> goods() {
		Map<String, Object> r = new HashMap<>();
		try {
			List<Goods> list = thisService.selectAll();
			for (Goods good : list) {
				Long id = good.getId();
				List<Foods> flist = foodsService.selectAllByGoodsId(id);
				good.setFoodss(flist);
				for (Foods food : flist) {
					Long fid = food.getId();
					List<Ratings> rlist = ratingsService.selectAllByGoodsId(fid);
					food.setRatingss(rlist);
				}
			}
			r.put("data", list);
			r.put("errno", 0);
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			r.put("data", null);
			r.put("errno", 1);
			return r;
		}
	}
	
	@ResponseBody
	@RequestMapping("/ratings")
	public Map<String, Object> ratings() {
		Map<String, Object> r = new HashMap<>();
		try {
			List<SellerRatings> list = sellerRatingsService.selectAll();
			r.put("data", list);
			r.put("errno", 0);
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			r.put("data", null);
			r.put("errno", 1);
			return r;
		}
	}
	
	@ResponseBody
	@RequestMapping("/seller")
	public Map<String, Object> seller() {
		Map<String, Object> r = new HashMap<>();
		try {
			List<Seller> list = sellerService.selectAll();
			r.put("data", list.get(0));
			r.put("errno", 0);
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			r.put("data", null);
			r.put("errno", 1);
			return r;
		}
	}

}